package com.lpf.back.controller;

import com.lpf.back.common.CommonRes;
import com.lpf.back.domain.Activity;
import com.lpf.back.domain.Comment;
import com.lpf.back.domain.Dynamic;
import com.lpf.back.domain.User;
import com.lpf.back.domain.vo.UserCommentVO;
import com.lpf.back.domain.vo.UserDynamicVO;
import com.lpf.back.service.impl.UserServiceImp;
import com.lpf.back.service.interfaces.DynamicService;
import com.lpf.back.service.interfaces.UserService;
import org.apache.catalina.core.ApplicationPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private DynamicService dynamicService;

    @RequestMapping("/login")
    public CommonRes login(String username, String password){
        return userService.login(username, password);
    }

    @RequestMapping("/register")
    public int register(User user){
        return userService.register(user);
    }

    @RequestMapping("/getInfo")
    public User getInfo(String username){
        return userService.getInfo(username);
    }

    @RequestMapping("/userLikeAct")
    public void likeActivity(User user, Activity activity){
        userService.userLikeActivity(user, activity);
    }

    @RequestMapping(value = "/setNickName", method = RequestMethod.POST)
    public CommonRes setNickName(String nickname, Integer id){
        int res = userService.setNickName(nickname, id);
        return new CommonRes(null, res, null);
    }

    @RequestMapping("/changePassword")
    public CommonRes changePassword(String password, Integer id){
        int res = userService.changePassword(password, id);
        return new CommonRes(null, res, null);
    }

    @RequestMapping("/setAvatar")
    public CommonRes setAvatar(HttpServletRequest request) throws Exception{
        return userService.setAvatar(request);
    }

    @RequestMapping("/dynamicList")
    public CommonRes dynamicList(){
        CommonRes res = new CommonRes();
        List<UserDynamicVO> dynamicList = dynamicService.dynamicList();
        res.setData(dynamicList);
        res.setStatus(1);
        res.setMsg("查询成功！");
        return res;
    }

    @RequestMapping("/likes")
    public CommonRes likes(Integer dynamicid, Integer userid){
        Dynamic dynamic = userService.likes(dynamicid, userid);
        if(dynamic != null){
            return new CommonRes(dynamic, 1, "点赞成功！");
        }
        return new CommonRes(null, 0, "无法重复点赞");
    }

    @RequestMapping(value = "/comment", method = RequestMethod.GET)
    public CommonRes comment(Integer dynamicid){
        List<UserCommentVO> comments = userService.comment(dynamicid);
        if(comments != null){
            return new CommonRes(comments, 1, "评论成功返回!");
        }
        return new CommonRes(null, 0, "该动态下没有评论!");
    }

    @RequestMapping(value = "/commitComment", method = RequestMethod.POST)
    public CommonRes commitComment(Comment comment){
        Comment res = userService.commitComment(comment);
        return new CommonRes(res, 1, "评论成功!");
    }
}
